var e = require;
var t = module;
var o = exports;
var ii, nn = this && this.__extends || (ii = function (t, e) {
    return (ii = Object.setPrototypeOf || {
        __proto__: []
    }
        instanceof Array &&
        function (t, e) {
            t.__proto__ = e
        } ||
        function (t, e) {
            for (var o in e) Object.prototype.hasOwnProperty.call(e, o) && (t[o] = e[o])
        })(t, e)
},
    function (t, e) {
        function o() {
            this.constructor = t
        }
        ii(t, e),
            t.prototype = null === e ? Object.create(e) : (o.prototype = e.prototype, new o)
    }
),
    rr = this && this.__awaiter ||
        function (t, e, o, i) {
            return new (o || (o = Promise))(function (n, a) {
                function r(t) {
                    try {
                        l(i.next(t))
                    } catch (e) {
                        a(e)
                    }
                }
                function s(t) {
                    try {
                        l(i.
                            throw(t))
                    } catch (e) {
                        a(e)
                    }
                }
                function l(t) {
                    var e;
                    t.done ? n(t.value) : (e = t.value, e instanceof o ? e : new o(function (t) {
                        t(e)
                    })).then(r, s)
                }
                l((i = i.apply(t, e || [])).next())
            })
        };
var oo = (this && this.__decorate) ||
    function (e, t, a, n) {
        var i,
            o = arguments.length,
            s = o < 3 ? t : null === n ? (n = Object.getOwnPropertyDescriptor(t, a)) : n;
        if ("object" == typeof Reflect && "function" == typeof Reflect.decorate) s = Reflect.decorate(e, t, a, n);
        else
            for (var r = e.length - 1; r >= 0; r--)
                (i = e[r]) && (s = (o < 3 ? i(s) : o > 3 ? i(t, a, s) : i(t, a)) || s);
        return o > 3 && s && Object.defineProperty(t, a, s), s;
    }; Object.defineProperty(o, "__esModule", { value: !0 });
var a = (function () {
    function e() {
        this._resMap = new Map();
    }
    return (
        (e.getInstance = function () {
            return this._resLoader || (this._resLoader = new e()), this._resLoader;
        }),
        (e.destroy = function () {
            this._resLoader && (this._resLoader = null);
        }),
        (e.prototype._getResItem = function (e, t) {
            var o = cc.loader,
                a = o._cache[e];
            if (!a) {
                var n = o._getResUuid(e, t, !0);
                if (n) {
                    var i = o._getReferenceKey(n);
                    a = o._cache[i];
                }
            }
            return a;
        }),
        (e.prototype._makeLoadResArgs = function () {
            if (arguments.length < 1 || "string" != typeof arguments[0])
                return console.error("_makeLoadResArgs error " + arguments), null;
            for (var e = { url: arguments[0] }, t = 1; t < arguments.length; ++t)
                1 == t && cc.js.isChildClassOf(arguments[t], cc.RawAsset)
                    ? (e.type = arguments[t])
                    : t == arguments.length - 1 && "string" == typeof arguments[t]
                        ? (e.use = arguments[t])
                        : "function" == typeof arguments[t] &&
                        (arguments.length > t + 1 && "function" == typeof arguments[t + 1]
                            ? (e.onProgess = arguments[t])
                            : (e.onCompleted = arguments[t]));
            return e;
        }),
        (e.prototype._makeReleaseResArgs = function () {
            if (arguments.length < 1 || "string" != typeof arguments[0])
                return console.error("_makeReleaseResArgs error " + arguments), null;
            for (var e = { url: arguments[0] }, t = 1; t < arguments.length; ++t)
                "string" == typeof arguments[t] ? (e.use = arguments[t]) : (e.type = arguments[t]);
            return e;
        }),
        (e.makeUseKey = function (e, t, o) {
            return void 0 === t && (t = "none"), void 0 === o && (o = ""), "use_" + e + "_by_" + t + "_for_" + o;
        }),
        (e.prototype.getCacheInfo = function (e) {
            return this._resMap.has(e) || this._resMap.set(e, { refs: new Set(), uses: new Set() }), this._resMap.get(e);
        }),
        (e.prototype.loadRes = function () {
            var e = this,
                t = this._makeLoadResArgs.apply(this, arguments);
            console.time("loadRes|" + t.url);
            var o = function (o, a) {
                var n = function (t, o) {
                    if (t && t.dependKeys && Array.isArray(t.dependKeys))
                        for (var a = 0, i = t.dependKeys; a < i.length; a++) {
                            var r = i[a];
                            e.getCacheInfo(r).refs.add(o);
                            var s = cc.loader._cache[r];
                            n(s, o);
                        }
                },
                    i = e._getResItem(t.url, t.type);
                if ((i && i.url && n(i, i.url), i)) {
                    var r = e.getCacheInfo(i.url);
                    r.refs.add(i.url), t.use && r.uses.add(t.use);
                }
                console.timeEnd("loadRes|" + t.url), t.onCompleted && t.onCompleted(o, a);
            },
                a =
                    (new Date().getTime(),
                        function (e, o, a) {
                            t.onProgess && t.onProgess(e, o, a);
                        }),
                n = cc.loader.getRes(t.url, t.type);
            n ? o(null, n) : cc.loader.loadRes(t.url, t.type, a, o);
        }),
        (e.prototype.releaseRes = function () {
            var e = this._makeReleaseResArgs.apply(this, arguments),
                t = this._getResItem(e.url, e.type);
            if (t) {
                var o = this.getCacheInfo(t.url);
                e.use && o.uses.delete(e.use), this._release(t, t.url);
            } else console.warn("releaseRes item is null " + e.url + " " + e.type);
        }),
        (e.prototype._release = function (e, t) {
            var o = this;
            if (e) {
                var a = this.getCacheInfo(e.url);
                a.refs.delete(t),
                    0 == a.uses.size &&
                    0 == a.refs.size &&
                    ((function (e, t) {
                        if (e && e.dependKeys && Array.isArray(e.dependKeys))
                            for (var a = 0, n = e.dependKeys; a < n.length; a++) {
                                var i = n[a],
                                    r = cc.loader._cache[i];
                                o._release(r, t);
                            }
                    })(e, t),
                        e.uuid
                            ? (cc.loader.release(e.uuid), cc.log("resloader release item by uuid :" + e.url))
                            : (cc.loader.release(e.url), cc.log("resloader release item by url:" + e.url)));
            }
        }),
        (e.prototype.checkReleaseUse = function () {
            var e = this._makeReleaseResArgs.apply(this, arguments),
                t = this._getResItem(e.url, e.type);
            if (!t) return console.log("cant release,item is null " + e.url + " " + e.type), !0;
            var o = this.getCacheInfo(t.url),
                a = !1,
                n = !1;
            return (
                (a = !(e.use && o.uses.size > 0 && (1 != o.uses.size || !o.uses.has(e.use)))),
                (n = !!((1 == o.refs.size && o.refs.has(t.url)) || 0 == o.refs.size)),
                a && n
            );
        }),
        (e._resLoader = null),
        e
    );
})();
o.default = a;
